import * as speechCommands from '@tensorflow-models/speech-commands'
const MODEL_URL = 'http://127.0.0.1:8080'
let transferRecognizer;

window.onload = async() => {
    const recongnizer = speechCommands.create(
        'BROWSER_FFT',
        null,
        MODEL_URL + '/speech/model.json',
        MODEL_URL + '/speech/metadata.json',
    )
    await recongnizer.ensureModelLoaded()
    transferRecognizer = recongnizer.createTransfer('轮播图')
    const res = await fetch(MODEL_URL + '/slider/data.bin')
    const arrayBuffer = await res.arrayBuffer()
    transferRecognizer.loadExamples(arrayBuffer)
    console.log(transferRecognizer.countExamples())
    await transferRecognizer.train({
        epochs: 30
    })
    console.log('done')
}
window.toggleTraining = async (isTraining) => {
    if (isTraining) {
        await transferRecognizer.listen(result => {
            const { scores } = result
            const labels = transferRecognizer.wordLabels()
            const index = scores.indexOf(Math.max(...scores))
            const label = labels[index]
            console.log(label)
        },{
            overlapFactor: 0,
            probabilityThreshold: 0.7
        })
    } else {
        transferRecognizer.stopListening()
    }
}
